package com.bwie.redkids.fragment;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;

import com.bwie.redkids.DetailsActivity;
import com.bwie.redkids.R;
import com.bwie.redkids.adapter.ClassifyGridViewBaseAdapter;
import com.bwie.redkids.adapter.ClassilyBaseAdapter;
import com.bwie.redkids.bean.Class_gv_Bean;
import com.bwie.redkids.bean.Class_gv_Bean.Productlist;
import com.bwie.redkids.bean.ClassifyBean;
import com.bwie.redkids.bean.ClassifyBean.ClassifyCategoryBean;
import com.bwie.redkids.utils.GsonUtils;
import com.bwie.redkids.utils.PathUtils;
import com.google.gson.Gson;

public class BrandFragment extends Fragment {
	protected static final int SUCESS_L = 0;
	protected static final int SUCESS_R = 1;
	private ListView brand_listView;
	private GridView gv_brand;
	private ArrayList<ClassifyCategoryBean> arrayList = new ArrayList<ClassifyBean.ClassifyCategoryBean>();
	private ArrayList<Productlist> arrayList2 = new ArrayList<Class_gv_Bean.Productlist>();
	/**
	 * 接受并展示数据
	 * */
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == SUCESS_L) {
				ClassifyBean classifyBean = (ClassifyBean) msg.obj;
				arrayList.addAll(classifyBean.getCategory());
				ClassilyBaseAdapter classilyBaseAdapter = new ClassilyBaseAdapter(
						getActivity(), arrayList);
				brand_listView.setAdapter(classilyBaseAdapter);
			}
			if (msg.what == SUCESS_R) {
				Class_gv_Bean class_gv_Bean = (Class_gv_Bean) msg.obj;
				arrayList2.addAll(class_gv_Bean.getProductlist());
				gv_brand.setAdapter(new ClassifyGridViewBaseAdapter(
						getActivity(), arrayList2));
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.brand_fragment, null, false);
		brand_listView = (ListView) view.findViewById(R.id.brand_listView);
		gv_brand = (GridView) view.findViewById(R.id.gv_brand);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getInformation();
		getGridViewInformation();
		gv_brand.setAdapter(new ClassifyGridViewBaseAdapter(getActivity(),
				arrayList2));
		gv_brand.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(getActivity(), DetailsActivity.class);
				intent.putExtra("id", arrayList.get(arg2).getId());
				startActivity(intent);

			}
		});
	}

	private void getGridViewInformation() {
		brand_listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				if (view.isClickable()) {
					view.setBackgroundColor(Color.RED);
				} else {
					view.setBackgroundColor(Color.BLACK);
				}

				arrayList2.clear();
				new Thread() {
					public void run() {
						try {
							String data = GsonUtils.getData(PathUtils.PATH
									+ PathUtils.PRODUCT_GOODS);
							Gson gson = new Gson();
							Class_gv_Bean fromJson1 = gson.fromJson(data,
									Class_gv_Bean.class);
							handler.obtainMessage(SUCESS_R, fromJson1)
									.sendToTarget();
						} catch (ClientProtocolException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					};
				}.start();

			}
		});
	}

	private void getInformation() {
		new Thread() {
			public void run() {
				try {
					String data = GsonUtils.getData(PathUtils.PATH
							+ PathUtils.CLASSIFY);
					Gson gson = new Gson();
					ClassifyBean fromJson = gson.fromJson(data,
							ClassifyBean.class);
					handler.obtainMessage(SUCESS_L, fromJson).sendToTarget();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

}
